package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 改密界面
 */
public class ChangePwdPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon chang = new ImageIcon("Tu/buttonModify.jpg");
    private JLabel accountLabel = new JLabel("修改密码：");
    private JLabel accountLabel1 = new JLabel("新密码：");
    private JPasswordField pwdLabel = new JPasswordField();
    private JPasswordField pwdLabel1 = new JPasswordField();

    private Font font = new Font("黑体", Font.BOLD, 30);//   设置字体大写


    private JButton loginBtn = new JButton(chang);


    public ChangePwdPanel(ATMFrame atmFrame) {
        this.atmFrame = atmFrame;
        this.setLayout(null);
        accountLabel.setBounds(250, 200, 220, 50);
        accountLabel.setFont(font);
        pwdLabel.setBounds(400, 200, 250, 50);    //输入空格
        accountLabel.setFont(font);
        accountLabel1.setBounds(250, 260, 250, 50);
        accountLabel1.setFont(font);
        pwdLabel1.setBounds(400, 260, 250, 50);    //输入空格
        accountLabel1.setFont(font);


        loginBtn.setBounds(700, 350, 140, 50);
        loginBtn.addActionListener(this);


        this.add(accountLabel);
        this.add(pwdLabel);
        this.add(accountLabel1);
        this.add(pwdLabel1);
        this.add(loginBtn);


        this.setOpaque(false);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        User user = atmFrame.getLoginUser();
        if (e.getSource()==loginBtn){
            if (!new String(pwdLabel.getPassword()).equals(user.getPwd()));
            JOptionPane.showMessageDialog(this,"原密码错误");
            return;
        }
        if (new String(pwdLabel1.getPassword()).equals("")){
            JOptionPane.showMessageDialog(this,"请输入新密码");
            return;
        }

        if (new String(pwdLabel1.getPassword()).equals(user.getPwd())){
            JOptionPane.showMessageDialog(this,"不能和原密码一样");
            return;
        }
        if (!new String(pwdLabel.getPassword()).equals(new String(pwdLabel1.getPassword()))){
            JOptionPane.showMessageDialog(this,"两次密码不一致");
            return;
        }
        user.setPwd(new String(pwdLabel1.getPassword())+"");
        JOptionPane.showMessageDialog(this,"密码修改成功");
        atmFrame.switchPanel("login");



    }
}